package com.zhfi.dataapply.schedule.writer.chaincode;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhfi.dataapply.entity.data.po.chaincode.EventTrendPO;
import com.zhfi.dataapply.mapper.chaincode.EventTrendMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class EventTrendWriter implements ItemWriter<EventTrendPO> {

    @Autowired
    private EventTrendMapper mapper;

    @Override
    public void write(List<? extends EventTrendPO> list) throws Exception {
        for (EventTrendPO po : list) {
            try {
                UpdateWrapper<EventTrendPO> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("chain_code", po.getChainCode());
                updateWrapper.eq("year", po.getYear());
                if (mapper.update(po, updateWrapper) <= 0) {
                    mapper.insert(po);
                }
            } catch (Exception e) {
                log.error("EventTrendWriter写入数据失败：" + po, e);
            }
        }
    }
}
